package com.kehao.leetcode.jzoffer;

import org.junit.Test;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/6/8 15:04
 */
public class JZSolution005 {
    public int maxProduct(String[] words) {
        int result = 0;
        int[] marks = new int[words.length];
        for (int i = 0; i < words.length; i++) {
            for (int j = 0; j < words[i].length(); j++) {
                marks[i] = marks[i] | (1<<(words[i].charAt(j)-'a'));
            }
        }

        for (int i = 0; i < words.length; i++) {
            int str2length = 0;
            for (int j = i+1; j < words.length; j++) {
                if(words[j].length()>str2length){
                    if(containsSameChar(marks,i,j)){
                        continue;
                    }else {
                        result = Integer.max(result,words[i].length()*words[j].length());
                    }
                }
            }
        }
        return result;
    }

    boolean containsSameChar(int[] marks, int i, int j){
        return (marks[i]&marks[j])!=0;
    }

    @Test
    public void test01(){
        String[] words = {"abcw","baz","foo","bar","fxyz","abcdef"};
        int result = maxProduct(words);
        System.out.println(result);
    }


    @Test
    public void test02(){
        String[] words = {"a","aa","aaa","aaaa"};
        int result = maxProduct(words);
        System.out.println(result);
    }
}
